import base64

import httpx
from langchain.chat_models import ChatOpenAI
from langchain.schema.messages import HumanMessage



def test():
    vision_llm = ChatOpenAI(
        api_key="sk-VowKQBUMIkSND8WScNJtDLqf3FyqWHQ43LMVUXH1m6GZaopA",
        base_url="https://ai.nengyongai.cn/v1",
        model="gpt-4-vision-preview",
        max_tokens=100)


    # 图片 URL
    image_url = "https://pics4.baidu.com/feed/faf2b2119313b07ea28d417738f7372c97dd8c48.jpeg"
    # image_url = "https://pics1.baidu.com/feed/4a36acaf2edda3cc7b7d1a5e35c99f0e203f92e6.jpeg"
    #
    message = [
        HumanMessage(
            content=[
                {
                    "type": "text",
                    "text": "请描述这张图片的内容"},
                {
                    "type": "image_url",
                    "image_url": {"url": image_url}}
            ]
        )
    ]
    #
    resp = vision_llm.invoke(message)

    print(resp.content)


def test2():
    image_url = "https://pics4.baidu.com/feed/faf2b2119313b07ea28d417738f7372c97dd8c48.jpeg"

    # 使用API代理服务提高访问稳定性
    image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")

    model = ChatOpenAI(
        api_key="sk-VowKQBUMIkSND8WScNJtDLqf3FyqWHQ43LMVUXH1m6GZaopA",
        base_url="https://ai.nengyongai.cn/v1",
        model="gpt-4o"
    )

    message = HumanMessage(
        content=[
            {
                "type": "text",
                "text": "图片都有什么动物"
            },
            {
                "type": "image_url",
                "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
            },
        ],
    )

    response = model.invoke([message])
    print(response.content)
